package com.hxzy.controller.tzxm;

import com.hxzy.common.annotation.Log;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.SysDictData;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.system.domain.dto.ValueExistsDTO;
import com.hxzy.tzxm.domain.CmsCategory;
import com.hxzy.tzxm.service.CmsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "栏目分类API")
@RestController
@RequestMapping(value = "/api/cms/category")
public class CmsCategoryController  extends BaseController {

    @Autowired
    private CmsCategoryService cmsCategoryService;


    @PreAuthorize("@hxzy.hasPermi('cms:category:list')")
    @ApiOperation(value = "分类列表")
    @GetMapping(value = "/list")
    public R search(){
        List<CmsCategory> categoryList = this.cmsCategoryService.search();
        List<CmsCategory> treeList= this.cmsCategoryService.buildTree(categoryList);
        return R.okHasData(treeList);
    }

    @ApiOperation(value = "查询栏目详细")
    @PreAuthorize("@hxzy.hasPermi('cms:category:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable Long id)
    {
        CmsCategory cmsCategory=this.cmsCategoryService.getById(id);
        cmsCategory.setUpdateBy(null);
        cmsCategory.setUpdateTime(null);
        return R.okHasData(cmsCategory);
    }


    @ApiOperation(value = "新增栏目")
    @PreAuthorize("@hxzy.hasPermi('cms:category:add')")
    @Log(title = "栏目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody CmsCategory cmsCategory)
    {

        ValueExistsDTO pinYingExistsDTO=new ValueExistsDTO(cmsCategory.getId(),cmsCategory.getCategoryPinyin());
        int count = this.cmsCategoryService.checkPinYingUnique(pinYingExistsDTO);

        if (count>0) {
            R r=R.build(AckCode.FAIL);
            r.setMsg("新增'" + cmsCategory.getCategoryPinyin() + "'失败，该值已存在");
            return r;
        }

        cmsCategory.setDelFlag("0");
        return toAjax(this.cmsCategoryService.save(cmsCategory));
    }

    @ApiOperation(value = "修改栏目")
    @PreAuthorize("@hxzy.hasPermi('cms:category:edit')")
    @Log(title = "栏目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R update(@Validated @RequestBody CmsCategory cmsCategory)
    {
        ValueExistsDTO pinYingExistsDTO=new ValueExistsDTO(cmsCategory.getId(),cmsCategory.getCategoryPinyin());
        int count = this.cmsCategoryService.checkPinYingUnique(pinYingExistsDTO);

        if (count>0) {
            R r=R.build(AckCode.FAIL);
            r.setMsg("修改'" + cmsCategory.getCategoryPinyin() + "'失败，该值已存在");
            return r;
        }
        return toAjax(this.cmsCategoryService.updateById(cmsCategory));
    }

    @ApiOperation(value = "删除栏目类型")
    @PreAuthorize("@hxzy.hasPermi('cms:category:remove')")
    @Log(title = "栏目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Long id)
    {
        int count= this.cmsCategoryService.delete(id);
        return super.toAjax(count);
    }
}
